package com.patchmanage.back.service.impl;

import cn.hutool.core.lang.Dict;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.patchmanage.back.dto.InventoryDto;
import com.patchmanage.back.entity.Inventory;
import com.patchmanage.back.entity.InventoryGroup;
import com.patchmanage.back.mapper.InventoryMapper;
import com.patchmanage.back.service.IInventoryGroupService;
import com.patchmanage.back.service.IInventoryService;
import com.patchmanage.back.utils.IpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huizhao
 * @since 2021-03-20
 */
@Service
public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory> implements IInventoryService {


    @Autowired
    public IInventoryService inventoryService;

    @Autowired
    public IInventoryGroupService inventoryGroupService;
    /**
     * 通过定义规则，给未分组的资产分组
     */
    public void groupForInventorys(){

        /** 获取所有分组规则**/
        QueryWrapper<InventoryGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("policy_status", 1);
        queryWrapper.orderByDesc("priority");
        List<InventoryGroup>  inventoryGroupList = inventoryGroupService.list(queryWrapper);

        //取出所有的ip规则
        List<JSONObject> jsonObjectList = new LinkedList<>();
        for(InventoryGroup inventoryGroup : inventoryGroupList){
            String matchRules = inventoryGroup.getMatchRules();
            //{"ip":[{"type":1,"value": "192.168.10.15"},{"type":3,"value": "192.168.0.1-192.168.0.100"},{"type":2,"value": "192.168.0.1/24"}]}
            JSONObject jsonObject = JSONUtil.parseObj(matchRules);
            jsonObject.put("groupId", inventoryGroup.getGroupId());
            jsonObjectList.add(jsonObject);
        }

        /** 获取所有未分组的资产**/
        QueryWrapper<Inventory> inventoryQueryWrapper = new QueryWrapper<>();
        inventoryQueryWrapper.isNull("group_id");
        inventoryQueryWrapper.orderByAsc("utime");
        List<Inventory>  inventoryList = inventoryService.list(inventoryQueryWrapper);
        for(Inventory inventory : inventoryList){
            String ip = inventory.getIp();
            for(JSONObject jsonObject : jsonObjectList){
                JSONArray jsonArray = jsonObject.getJSONArray("ip");
                JSONObject jsonCObject;
                for(Object j : jsonArray){
                    jsonCObject = JSONUtil.parseObj(j);
                    Integer type = jsonCObject.getInt("type");
                    String ipRule = jsonCObject.getStr("value");
                    Integer groupId = jsonObject.getInt("groupId");

                    Boolean isMatch = false;
                    if(type == 1 && ip.equals(ipRule)){
                        isMatch = true;
                    }else if(type == 2 && IpUtil.isInRange(ip, ipRule)){
                        isMatch = true;
                    }else if(type == 3 && IpUtil.ipIsValid(ip, ipRule)){
                        isMatch = true;
                    }
                    if(isMatch){
//                        inventory.setGroupId(groupId);
                        UpdateWrapper<Inventory> updateWrapper = new UpdateWrapper<>();
                        updateWrapper.set("group_id", groupId);
                        updateWrapper.eq("is_available",1);
                        inventoryService.update(updateWrapper);
                    }
                }
            }
        }
    }

    public IPage<InventoryDto> listPageEx(IPage<InventoryDto> page, Dict dict){
        return baseMapper.selectInventoryPage(page, dict);
    }

    public List<Inventory> listEx(Map<String, Object> map){
        return baseMapper.selectInventory(map);
    }
}
